package com.extremelylargepackage;

import java.util.Iterator;

import com.extremelylargepackage.entity.Conclusion;
import com.extremelylargepackage.entity.Enemy;
import com.extremelylargepackage.entity.Entity;
import com.extremelylargepackage.entity.Entity.Status;
import com.extremelylargepackage.entity.ScrollingBackground;

import android.content.Context;
import android.graphics.Canvas;
import android.view.MotionEvent;

public class GameEngine extends Engine
{
	private GameState _state;
	private Conclusion _conclusion;
	private ScrollingBackground _fore = new ScrollingBackground(GameResources.getResource("space_fore_nc"));

	public GameEngine(ThreadCallBack thread, String levelName, boolean local)
	{
		super(thread);
		_state = new GameState(levelName, local);
		_conclusion = new Conclusion();
		_fore.setScrolling(true);
		float speed = _state.getBackground().getDirY();
		_state.getBackground().setDirY(speed * .5f);
		_fore.setDirY(speed);
	}
	
	@Override
	protected void onInitialize() 
	{
		//Do nothing.
	}
	
	@Override
	protected void updateState(long interval)
	{
		_state.getLevel().updateLevel(interval);
		_conclusion.updateState(interval);
		checkGameOver();
		if(!_state.actionPaused())
		{
			_state.checkCollisions();
			_state.getBackground().updateState(interval);
			_fore.updateState(interval);
			_state.getPlayer().updateState(interval);
			
			Iterator<Enemy> enemyIt = _state.getEnemies().iterator();
			while(enemyIt.hasNext()) {
				Enemy enemy = enemyIt.next();
				enemy.updateState(interval);
	
				if(enemy.finished()) {
					_state.getJunkyard().add(enemy);
					enemyIt.remove();
				}
			}
		}

		Iterator<Entity> sceneryIt = _state.getScenery().iterator();
		while(sceneryIt.hasNext()) {
			Entity scenery = sceneryIt.next();
			scenery.updateState(interval);

			if(scenery.getStatus() == Status.dead) {
				_state.getJunkyard().add(scenery);
				sceneryIt.remove();
			}
		}
	 }
	
	private void checkGameOver()
	{
		if(_state.getLevel().levelComplete())
		{
			_conclusion.setConclusion(true);
		}
		if(_state.getPlayer().getHealth() <= 0)
		{
			_conclusion.setConclusion(false);
		}
		if(_conclusion.canExit())
		{
			_thread.levelFinished(_conclusion.getVictory());
		}
	}
	
	@Override
	public void draw(Canvas c) 
	{
		if(_initialized)
		{
			_state.getBackground().draw(c);
			_fore.draw(c);
			_state.getPlayer().draw(c);

			for(Entity enemies : _state.getEnemies())
			{
				enemies.draw(c);
			}

			for(Entity scenery : _state.getScenery())
			{
				scenery.draw(c);
			}
			_state.getHealthbar().draw(c);
			
			StatTracker.getInstance().drawOverlay(c);
			
			_conclusion.draw(c);
		}
	}

	@Override
	public void onTouch(MotionEvent event) 
	{
		if(event.getAction() != MotionEvent.ACTION_UP )
			_state.getPlayer().setDestination(event.getX(), event.getY() - 50);
		else
		{
			_state.getPlayer().setDirX(0);
			_state.getPlayer().setDirY(0);
		}
		if(event.getPointerCount() == 2)
			_state.getPlayer().fireSpecialWeapon();
	}

	public void saveState(Context context) {
		_state.saveState(context);
	}
}
